package Boot.test.Utils;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class Mailbox {
    private String emailHost = "smtp.qq.com";       //发送邮件的主机
    private String transportType = "smtp";           //邮件发送的协议
    private String fromEmail = "375156486@qq.com";  //发件人邮箱
    private String authCode = "ajqphveuqvcsbgbd";    //发件人邮箱授权码

    /**
     * @param Mailbox 收件人邮箱
     * @param Type 邮箱类型
     * @param Text  发送的信息
     * @throws MessagingException
     */
    public void SendMailbox(String Mailbox, String Type, String Text) throws MessagingException {
        String fromUser = Mailbox;           //发件人名称
        String toEmail = Mailbox;   //收件人邮箱
        String subject = "";
        String ContentTitle = "";
        switch (Type) {
            case "Register":
                ContentTitle = "获取注册时所需验证码";
                subject = "在线聊天系统获取验证码";
                break;
            case "Forgot":
                ContentTitle = "获取忘记密码时所需验证码";
                subject = "在线聊天系统获取验证码";
                break;
            case "Edit":
                ContentTitle = "获取修改密码时所需验证码";
                subject = "在线聊天系统获取验证码";
                break;
            case "Login":
                ContentTitle = "注册时分配给您的登录密码";
                subject = "在线聊天系统获取验证码";
                break;
        }
        //主题信息
        Properties props = new Properties();
        //设置邮件协议
        props.setProperty("mail.transport.protocol", transportType);
        // 设置发送邮件的主机
        props.setProperty("mail.host", emailHost);
        // 设置发送人邮箱
        props.setProperty("mail.user", fromUser);
        // 设置收件人姓名
        props.setProperty("mail.from", fromEmail);
        props.put("mail.smtp.port", 110);// 端口号  不要使用25
//        props.put("mail.smtp.auth", "true");  //记得开
//        props.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
        //2.根据属性获得一个会话
        Session session = Session.getInstance(props);
        //3.设置会话是debug模式(会打印更多相关信息)
//        session.setDebug(true);
        //4.创建邮件主题信息对象
        MimeMessage message = new MimeMessage(session);
        //5.设置发件人
        message.setFrom(new InternetAddress(fromEmail));
//        //6.设置邮件主题
        message.setSubject(subject);
//        //7.设置邮件正文
        // Text
        if(Type.equals("Login")){
            message.setContent("<h1 style='letter-spacing:2px'>" + ContentTitle + "</h1><p style='letter-spacing:1px'>您注册后分配的登录密码是：<span style='font-size:30px;color:red'>" + Text + "</span></p>", "text/html;charset=UTF-8");
        }else{
            message.setContent("<h1 style='letter-spacing:2px'>" + ContentTitle + "</h1><p style='letter-spacing:1px'>您所请求的验证码是：<span style='font-size:30px;color:red'>" + Text + "</span></p><p style='letter-spacing:1px'>您所请求的验证码有效期为<span style='color:red'>10分钟</span>，<span style='color:red'>10分钟</span>后验证码将失效，若此验证码不是您本人获取，请忽略该条信息</p>", "text/html;charset=UTF-8");
        }
//        //8.设置收件人: TO-发送    CC-抄送   BCC-密送
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
//        //9.获取发送器对象:提供指定的协议
        Transport transport = session.getTransport();
//        //10.设置发件人的信息
        transport.connect(emailHost, fromEmail, authCode);
//        //11.发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        //12.关闭资源
        transport.close();


    }

}
